package com.hxzy.controller.shengpi;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxzy.common.annon.Log;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.vo.R;
import com.hxzy.controller.admin.BaseController;
import com.hxzy.dto.shengpi.CategorySearchDTO;
import com.hxzy.dto.shengpi.ItemSearchDTO;
import com.hxzy.entity.shengpi.ShengpiCategory;
import com.hxzy.entity.shengpi.ShengpiProject;
import com.hxzy.service.shengpi.ShengpiProjectService;
import com.hxzy.vo.shengpi.ShengpiProjectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "审批项目列表API")
@RestController
@RequestMapping(value = "/api/xmsp/project")
public class SpProjectController extends BaseController {

    @Autowired
    private ShengpiProjectService shengpiProjectService;

    @PreAuthorize(value = "@ss.hasAuthority('xmsp:project:list')")
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R search(ItemSearchDTO searchDTO){
        super.startPageHelper();
        List<ShengpiProjectVO> list = this.shengpiProjectService.search(searchDTO);
        return super.pageToPageVO(list);
    }

    @ApiOperation(value = "新增")
    @PreAuthorize(value = "@ss.hasAuthority('xmsp:project:add')")
    @PostMapping
    @Log(title = "重庆投资项目",businessType = BusinessType.INSERT)
    public R add(@RequestBody @Valid ShengpiProject shengpiProject){
        boolean save = this.shengpiProjectService.save(shengpiProject);
        return super.toR(save);
    }

    @ApiOperation(value = "修改")
    @PreAuthorize(value = "@ss.hasAuthority('xmsp:project:edit')")
    @PutMapping
    @Log(title = "重庆投资项目",businessType = BusinessType.UPDATE)
    public R update(@RequestBody @Valid ShengpiProject shengpiProject){
        boolean save = this.shengpiProjectService.updateById(shengpiProject);
        return super.toR(save);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    @PreAuthorize(value = "@ss.hasAuthority('xmsp:project:query')")
    public R findById(@PathVariable(value = "id") Long id){
        ShengpiProject shengpiProject = this.shengpiProjectService.getById(id);
        if(shengpiProject==null){
            return  R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(shengpiProject);
    }

}
